package com.atguigu.state;


import org.apache.flink.api.common.functions.FlatMapFunction;
import org.apache.flink.api.common.state.ListState;
import org.apache.flink.api.common.state.ListStateDescriptor;
import org.apache.flink.configuration.Configuration;
import org.apache.flink.runtime.state.FunctionInitializationContext;
import org.apache.flink.runtime.state.FunctionSnapshotContext;
import org.apache.flink.streaming.api.checkpoint.CheckpointedFunction;
import org.apache.flink.streaming.api.datastream.DataStreamSource;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;
import org.apache.flink.util.Collector;

import java.util.ArrayList;
import java.util.List;

public class Flink02_Operator_UnionListState {
    public static void main(String[] args) {
        Configuration configuration = new Configuration();
        configuration.setInteger("rest.port",10000);
        StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment(configuration);
        env.setParallelism(2);

        //设置检查点周期
        env.enableCheckpointing(3000);

        DataStreamSource<String> ds = env.socketTextStream("hadoop162", 9999);

        //对数据进行处理
        ds.flatMap(new MyFlatMapFunction()).print();

        try {
            env.execute();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public static class MyFlatMapFunction implements FlatMapFunction<String,String>, CheckpointedFunction {

        private ListState<String> unionState;
        //创建临时保存状态的数组
        List<String> list = new ArrayList<String>();

        //继承自flatmapfunciton的方法，用来实现
        @Override
        public void flatMap(String value, Collector<String> out) throws Exception {

            //将所有的value处理
            String[] datas = value.split(" ");
            for (String data : datas) {

                //将结果放入临时数组中
                list.add(data);
            }
            out.collect(list.toString());



        }

        //该方法用来周期性的存储状态
        @Override
        public void snapshotState(FunctionSnapshotContext context) throws Exception {
            //将保存的临时状态保存到状态存储的数据结构中
            unionState.update(list);

        }

        @Override
        public void initializeState(FunctionInitializationContext context) throws Exception {
            //定义状态存储方式
            unionState = context
                    .getOperatorStateStore()
                    .getUnionListState(new ListStateDescriptor<String>("unionState", String.class));
            //初始化的时候将状态保存到临时状态上
            Iterable<String> iterator = unionState.get();
            for (String s : iterator) {
                list.add(s);
            }
        }
    }
}
